<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>维修管理</el-breadcrumb-item>
                <el-breadcrumb-item>维修厂</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="repairFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="repairFormData.name" placeholder="请输入维修厂名称"></el-input>
                </el-form-item>
<!--                <el-form-item >-->
<!--                    <el-input v-model="repairFormData.address" placeholder="请输入维修厂地址"></el-input>-->
<!--                </el-form-item>-->
<!--                <el-form-item >-->
<!--                    <el-input v-model="repairFormData.phoneNum" placeholder="请输入店铺电话"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item>
                    <el-button type="primary"  @click="initShopTable">查询</el-button>
                    <el-button type="primary" @click="tianjia">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            >
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="维修厂名称"
                            >
                    </el-table-column>
                    <el-table-column
                            prop="address"
                            label="地址">
                    </el-table-column>
                    <el-table-column
                            prop="phoneNum"
                            label="联系方式">
                    </el-table-column>
                    <el-table-column
                            prop="addtime"
                            label="添加时间"
                            >
                    </el-table-column>
                    <el-table-column
                            prop="remark"
                            label="备注">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="editShop(scope.row.id)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.id)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>
        <!--修改的对话框-->

        <el-dialog
                title="添加修改信息"
                :visible.sync="editDialogVisible"
                width="40%"
                :before-close="qx">
            <!--model:表单数据-->
            <el-form  label-width="100px" :model="repairForm" :rules="rules" ref="ruleForm" >
                <el-form-item label="维修厂ID" prop="id" style="display: none">
                    <el-input v-model="repairForm.id"></el-input>
                </el-form-item>
                <el-form-item label="维修厂名称" prop="name">
                    <el-input v-model="repairForm.name"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="repairForm.address"></el-input>
                </el-form-item>
                <el-form-item  label="联系方式" prop="phoneNum">
                    <el-input v-model.number="repairForm.phoneNum"></el-input>
                </el-form-item>
<!--                <el-form-item label="添加时间">-->
<!--                    <el-input v-model="repairForm.addtime"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="备注">
                    <el-input v-model="repairForm.remark"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="qx">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
              </span>
        </el-dialog>
    </div>
</template>

<script>

    export default {
        name: "Repair",
        data(){
                //维修厂名的唯一校验
            let  ShopName = (rule,value,callback) =>{
                    this.$http.post("/safeguard/maintainShop/selectByName",this.repairForm).then(res =>{

                        console.log(res);
                               if (res.data.code==2000){
                                   callback(res.data.message);
                               }else {
                                   callback();
                               }
                            });
            }




            return{
                repairFormData:{},
                tableData:[],
                editDialogVisible:false,
                repairForm:{},
                currentPage:1,
                pageSize:5,
                total:0,

                ruleForm: {
                    name:'',
                    address:'',
                    phoneNum:'',
                },

                rules: {
                    name: [
                        { required: true, message: '请输入维修厂名称', trigger: 'blur' },
                        { validator: ShopName, trigger: 'blur' },

                    ],
                    address: [
                        { required: true, message: '请输入维修厂地址', trigger: 'blur' },
                    ],
                    phoneNum: [

                        { required: true, message: '手机号不能为空',trigger: 'blur'},

                        { validator:function(rule,value,callback){
                                if(/^1[34578]\d{9}$/.test(value) == false){
                                    callback(new Error("请输入正确的手机号"));
                                }else{
                                    callback();
                                }
                            }, trigger: 'blur'
                        },

                    ]
                }
            }
        },
        created() {
            this.initShopTable();
        },
        methods:{
            //分页查询
            initShopTable(){
                //get方式不支持json数据
                var that=this;
                this.$http.post(`/safeguard/maintainShop/initMaintainShopTable/${this.currentPage}/${this.pageSize}`,this.repairFormData).then(function(resp){
                    // console.log(resp);
                    that.tableData=resp.data.result.records;
                    that.total=resp.data.result.total;
                });

            },
            //添加
            tianjia(){
                this.editDialogVisible=true;
                this.repairForm={};
            },
            //删除
            deleteById(id){

                var that=this;
                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    this.$http.delete(`/safeguard/maintainShop/deleteById/${id}`).then(function (resp) {
                        that.$message.success(resp.data.message);
                        that.initShopTable();
                        // location.reload();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
                this.currentPage=1;


            },
            //修改
            editShop(id){
                this.editDialogVisible=true;
                var that=this;
                this.$http.get(`/safeguard/maintainShop/selectById/${id}`).then(function(resp){
                    console.log(resp)
                    that.repairForm=resp.data;
                })
            },
            //提交
            submit(){
                var that=this;
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        if(this.repairForm.id!=null){
                            //JSON对象
                            //修改提交
                            this.$http.put(`/safeguard/maintainShop/update`,this.repairForm).then(function(resp){
                                console.log(resp)
                                if(resp.data.success==true){
                                    that.editDialogVisible=false;
                                    that.$message.success(resp.data.message);
                                    that.initShopTable();
                                }
                            })
                        }else{
                            //添加提交
                            this.$http.post(`/safeguard/maintainShop/insertMaintainShop`,this.repairForm).then(function (resp) {
                                if(resp.data.success==true){
                                    console.log(resp)
                                    that.editDialogVisible=false;
                                    that.$message.success(resp.data.message);
                                    that.initShopTable();
                                }
                            })
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },


            //取消
            qx(){
                this.$refs.ruleForm.resetFields();
                this.editDialogVisible=false;
            },

            // selectByName(){
            //     //get方式不支持json数据
            //     var that=this;
            //     this.$http.post(`/safeguard/maintainShop/selectByName/${this.name}/${this.id}`).then(function(resp){
            //         console.log(resp);
            //         // that.tableData=resp.data.result.records;
            //         // that.total=resp.data.result.total;
            //     });
            //
            // },

            handleSizeChange(v){
                this.pageSize=v;
                this.initShopTable();
            },
            handleCurrentChange(v){
                this.currentPage=v;
                this.initShopTable();
            },

        },
    }
</script>

<style scoped>

</style>